﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HDJ.Framework.Core;
using UnityEngine;

namespace HDJ.Framework.Core.Editor
{
    public class TableDataDrawLanguageField : TableDataDrawFieldBase
    {
        private List<string> langKeys;

        public override Type FieldType =>typeof(string);

        public override string Tag => "Language";

        public override void Init()
        {
            EditorFramework.OnLanguageDataChange += OnLanguageDataChange;
            OnLanguageDataChange();
        }

        private void OnLanguageDataChange()
        {
            if (ResourceManager.ExistAssets(LanguageManager.LanguagesSettingFileName))
            {
                LanguageSettingData settingData = LanguageManager.LoadLocalizedLanguageSettingData();
                langKeys = LanguageUtils.GetAllFullKeys(settingData.defaultLanguage);
            }
            else
            {
                langKeys = new List<string>();
            }
        }
        public override object GetShowValue(object value, TableFieldInfo info)
        {
            return LanguageManager.GetText(value.ToString());
        }
        public override object DrawGUI(string text, object value, TableFieldInfo info)
        {

            value = EditorDrawGUIUtil.DrawBaseValue(text, value);
            if (value!=null)
            {
                value = EditorDrawGUIUtil.DrawPopup(text, value.ToString(), langKeys);
                GUILayout.Space(6);
                GUILayout.Label("多语言字段[" + value + "] : " + LanguageManager.GetText(value.ToString()));
                GUILayout.Space(8);
                if (GUILayout.Button("打开编辑当前多语言字段"))
                {
                    LanguageEditorWindow w = LanguageEditorWindow.ShowWindow();
                    w.searchValue = value.ToString();
                    w.toolbarOption = 1;
                }
            }
            else
            {
                GUILayout.Space(6);
                GUILayout.Label("多语言字段为非null时支持多语言选项");
                GUILayout.Space(8);
            }
            return value;
        }
    }
}


